package com.ruoyi.contact.controller;

import java.util.List;

import com.ruoyi.contact.service.IContactCategoryService;
import com.ruoyi.contact.service.IContactInfoService;
import com.ruoyi.contact.service.IContactLevelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.contact.domain.ContactRecord;
import com.ruoyi.contact.service.IContactRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ShiroUtils;


/**
 * 联系记录Controller
 * 
 * @author ruoyi
 * @date 2025-05-30
 */
@Controller
@RequestMapping("/contact/record")
public class ContactRecordController extends BaseController
{
    private String prefix = "contact/record";

    @Autowired
    private IContactRecordService contactRecordService;
    @Autowired
    private IContactCategoryService contactCategoryService;
    @Autowired
    private IContactLevelService contactLevelService;
    @Autowired
    private IContactInfoService contactInfoService;

    @RequiresPermissions("contact:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询联系记录列表
     */
    @RequiresPermissions("contact:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ContactRecord contactRecord)
    {
        startPage();
        List<ContactRecord> list = contactRecordService.selectContactRecordList(contactRecord);
        return getDataTable(list);
    }

    /**
     * 导出联系记录列表
     */
    @RequiresPermissions("contact:record:export")
    @Log(title = "联系记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ContactRecord contactRecord)
    {
        List<ContactRecord> list = contactRecordService.selectContactRecordList(contactRecord);
        ExcelUtil<ContactRecord> util = new ExcelUtil<ContactRecord>(ContactRecord.class);
        return util.exportExcel(list, "联系记录数据");
    }

    /**
     * 新增联系记录
     */
    @RequiresPermissions("contact:record:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("contacts", contactInfoService.selectContactInfoList(null));
        mmap.put("userString", ShiroUtils.getSysUser().toUserString());
        return prefix + "/add";
    }

    /**
     * 新增保存联系记录
     */
    @RequiresPermissions("contact:record:add")
    @Log(title = "联系记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ContactRecord contactRecord)
    {
        contactRecord.setUserId(ShiroUtils.getUserId());
        return toAjax(contactRecordService.insertContactRecord(contactRecord));
    }

    /**
     * 修改联系记录
     */
    @RequiresPermissions("contact:record:edit")
    @GetMapping("/edit/{recordId}")
    public String edit(@PathVariable("recordId") Long recordId, ModelMap mmap)
    {
        ContactRecord contactRecord = contactRecordService.selectContactRecordByRecordId(recordId);
        mmap.put("contactRecord", contactRecord);
        mmap.put("userString", ShiroUtils.getSysUser().toUserString());
        return prefix + "/edit";
    }
    /**
     * 修改保存联系记录
     */
    @RequiresPermissions("contact:record:edit")
    @Log(title = "联系记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ContactRecord contactRecord)
    {
        contactRecord.setUserId(ShiroUtils.getUserId());
        return toAjax(contactRecordService.updateContactRecord(contactRecord));
    }

    /**
     * 删除联系记录
     */
    @RequiresPermissions("contact:record:remove")
    @Log(title = "联系记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(contactRecordService.deleteContactRecordByRecordIds(ids));
    }
}
